import axios from 'axios';

const cart = {
    state: { //类似data属性，存数据的地方,公共数据
        cartlist: []
    },
    getters: { //类似computed 计算属性，但只有get方法
        total(state) {
            return state.cartlist.length;
        },
        money(state) {
            let res = 0;
            state.cartlist.forEach(item => {
                res += item.price * item.num;
            });
            return res;
        }
    },
    mutations: { //类似methods方法
        getcart(state, data) { //获取购物车数据放到state

            // let newArr = []; //去除空对象
            // for (let j in data) {
            //     for (let prop in data[j]) {
            //         if (prop != '' || data[j][prop] != '') {
            //             newArr.push(data[j]);
            //         }
            //     }
            // }
            let hash = {};
            data = data.reduce((preVal, curVal) => {
                hash[curVal.cname] ? '' : hash[curVal.cname] = true && preVal.push(curVal);
                return preVal
            }, [])
            state.cartlist = data //newArr;
            console.log(state.cartlist);
            // console.log(data);



        },
        additem(state, data) { //添加新商品到state中
            state.cartlist.push(data)
        },
        updateitem(state, data) { //修改state中商品数据
            state.cartlist.forEach(item => {
                if (item.gid == data.id && item.uid == data.uid) {
                    item.num = data.num
                }
            })
        },

    },
    actions: { //类似vue里面的methods方法,里面的代码是异步的方法
        async additem(contex, good) {
            //发送ajax加入购物车
            let {
                gid,
                num,
                uid
            } = good;
            // console.log({gid,uid});

            //查询是否已经存在商品
            let {
                data: data1
            } = await axios.get('http://localhost:5555/listbo/checkcart', {
                params: {
                    gid,
                    uid
                }
            });

            // console.log(data1);
            if (data1.length == 0) {
                //不存在，添加商品
                // console.log(good);
                let {
                    data
                } = await axios.post('http://localhost:5555/listbo/addcart', good);
                //添加新数据也放到state里面
                contex.commit('additem', good);
                console.log(data);
                // console.log("不存在");


                return data;
            } else {
                //存在，修改数量
                let kucun = data1[0].kucun;
                let currentnum = data1[0].num;
                if (num + currentnum >= kucun) {
                    //超过库存量
                    num = kucun;
                    window.console.log('库存量已用完');
                } else {
                    num = num + currentnum;
                }
                //修改库存量
                let {
                    data
                } = await axios.put('http://localhost:5555/listbo/good', {
                    gid,
                    uid,
                    num
                });
                contex.commit('updateitem', {
                    gid,
                    uid,
                    num
                }); //修改数量
                return data;
            }
        },
        async getcartlsit(contex) { //获取库存中购物车数据放到state中


            let {
                data
            } = await axios.get('http://localhost:5555/listbo/cartlist', {
                params: {
                    uid: localStorage.getItem("user") * 1
                }
            });
            // console.log(data);
            contex.commit('getcart', data)
        },
        async updatedgood(contex, good) { //修改库存数量
            let {
                gid,
                uid,
                num
            } = good;
            let {
                data
            } = await axios.put('http://localhost:5555/listbo/good', {
                gid,
                uid,
                num
            });
            console.log(data);

            contex.commit('updateitem', {
                gid,
                uid,
                num
            }) //修改数量
        }
    }
}



export default cart;